package com.gongpingjialoan.activity.car;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.view.FastBuyCarDialog;

import java.util.Random;

/**
 * Created by Administrator on 2015/9/25.
 */
public class FastBuyCar extends AppCompatActivity implements View.OnClickListener {

    private TextView titleTextView;
    private Button closeButton;
    private ImageView imageView;
    private ObjectAnimator objectAnimator;
    private Random random;
    private int num;
    private static final int MESSAGE = 0x1AA1;
    private int currentNum = 1;
    private TextView tTextView, numTextView;

    private String priceType = "";
    private RelativeLayout privateRelativeLayout, dealerRelativeLayout;
    private FastBuyCarDialog fastBuyCarDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fast_buy_car);
        initView();
    }

    private void initView() {
        random = new Random();
        num = random.nextInt(4) + 5;
        priceType = getIntent().getStringExtra("priceType");
        tTextView = (TextView) this.findViewById(R.id.title);
        numTextView = (TextView) this.findViewById(R.id.number);
        fastBuyCarDialog = new FastBuyCarDialog(this);
        // backTextView.setText(Html.fromHtml("<u>" + "立即返回→" + "</u>"));
        privateRelativeLayout = (RelativeLayout) this.findViewById(R.id.private_relative);
        dealerRelativeLayout = (RelativeLayout) this.findViewById(R.id.dealer_relative);
        titleTextView = (TextView) this.findViewById(R.id.top_title);
        closeButton = (Button) this.findViewById(R.id.title_back);
        imageView = (ImageView) this.findViewById(R.id.image);

        titleTextView.setText("快速卖车");
        closeButton.setOnClickListener(this);

        if ("private_party".equals(priceType)) {
            tTextView.setText("正在为您查找符合您意向的买家");
            privateRelativeLayout.setVisibility(View.VISIBLE);
            dealerRelativeLayout.setVisibility(View.GONE);
            tTextView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    fastBuyCarDialog.show();
                }
            }, 1000);
        } else {
            tTextView.setText("正在为您查找符合您意向的商家");

            objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);
            objectAnimator.setDuration(num * 1000 - 500);
            objectAnimator.setInterpolator(new LinearInterpolator());
            objectAnimator.setRepeatCount(1);
            imageView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    objectAnimator.start();
                }
            }, 500);
            myHandler.sendEmptyMessageDelayed(MESSAGE, 1000);
        }

    }

    private android.os.Handler myHandler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MESSAGE:
                    numTextView.setText(currentNum + "");
                    currentNum++;
                    if (currentNum <= num) {
                        myHandler.sendEmptyMessageDelayed(MESSAGE, 1000);
                    } else {
                        objectAnimator.cancel();
                        fastBuyCarDialog.show();
                    }
                    break;
            }
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (objectAnimator != null) {
            objectAnimator.cancel();
            objectAnimator = null;
        }

        if (myHandler != null) {
            myHandler.removeMessages(MESSAGE);
        }
    }


    private void gomain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }


    @Override
    public void onClick(View view) {
        if (view == closeButton) {
            gomain();
        }
    }

    @Override
    public void onBackPressed() {
        //goSellCar();
        gomain();
    }
}
